
/**
 * 生成过滤器内容的工具函数
 * 
 * @param currencyCheckedList - 货币物品过滤列表
 * @param equipmentCheckedList - 装备物品过滤列表  
 * @param jewelCheckedList - 珠宝物品过滤列表
 * @param flaskCheckedList - 药剂物品过滤列表
 * @param skillGemCheckedList - 技能宝石过滤列表
 * @param uniqueCheckedList - 传奇物品过滤列表
 * @param normalEquipmentCheckedList - 普通装备过滤列表
 * @returns 生成的过滤器内容字符串。如果所有列表都为空则返回空字符串
 */

import { skillGemOptions } from '../components/options/SkillGemOptions';
import { generateRule, type FilterRuleOptions } from './rules';
import type { FilterItem } from '../components/ItemFilter/types';

function generateHeader(): string {
    return `# POE过滤器生成器\n# 生成时间: ${new Date().toLocaleString()}\n\n`;
}

function generateCurrencyRules(items: FilterItem[]): string {
    return items
        .map((item) => generateRule(generateRuleWithState(item, 'currency')))
        .filter(result => result.isValid && result.content.length > 0)
        .map(result => result.content)
        .join('\n\n');
}

// 更新规则生成函数的模板
function generateRuleWithState(item: FilterItem, type: string): FilterRuleOptions {
    return {
        label: item.label,
        value: item.value,
        soundEnabled: item.soundEnabled,
        type: type as FilterRuleOptions['type'],
        color: item.color,
        bgColor: item.bgColor,
        displayEnabled: item.displayEnabled ?? true
    };
}

function generateEquipmentRules(items: FilterItem[]): string {
    return items
        .map(item => generateRule(generateRuleWithState(item, 'equipment')))
        .filter(result => result.isValid && result.content.length > 0)
        .map(result => result.content)
        .join('\n\n');
}

function generateJewelRules(items: FilterItem[]): string {
    return items
        .map(item => generateRule(generateRuleWithState(item, 'jewel')))
        .filter(result => result.isValid && result.content.length > 0)
        .map(result => result.content)
        .join('\n\n');
}

function generateFlaskRules(items: FilterItem[]): string {
    return items
        .map(item => generateRule(generateRuleWithState(item, 'flask')))
        .filter(result => result.isValid && result.content.length > 0)
        .map(result => result.content)
        .join('\n\n');
}

function generateSkillGemRules(items: FilterItem[]): string {
    return items
        .map(item => {
            const option = skillGemOptions.find((opt) => opt.value === item.value && opt.label === item.label);
            const ruleOptions = generateRuleWithState(item, 'skillgem');
            ruleOptions.tap = option?.tap;
            return generateRule(ruleOptions);
        })
        .filter(result => result.isValid && result.content.length > 0)
        .map(result => result.content)
        .join('\n\n');
}

function generateUniqueRules(items: FilterItem[]): string {
    return items
        .map(item => generateRule(generateRuleWithState(item, 'unique')))
        .filter(result => result.isValid && result.content.length > 0)
        .map(result => result.content)
        .join('\n\n');
}

function generateNormalEquipmentRules(items: FilterItem[]): string {
    return items
        .map(item => generateRule(generateRuleWithState(item, 'normalequipment')))
        .filter(result => result.isValid && result.content.length > 0)
        .map(result => result.content)
        .join('\n\n');
}

function generateMiscRules(items: FilterItem[]): string {
    return items
        .map(item => generateRule(generateRuleWithState(item, 'misc')))
        .filter(result => result.isValid && result.content.length > 0)
        .map(result => result.content)
        .join('\n\n');
}

export function generateSingleItemFilter(item: FilterItem, type: string): string {
    const header = generateHeader();
    let rule = '';

    // 生成规则标题注释
    const ruleHeader = `# ====================================\n`;
    const ruleTitle = `# ${item.label} 过滤规则\n`;
    const ruleStatus = `# 显示: ${item.displayEnabled ? '开启' : '关闭'}\n`;
    const ruleDivider = `# ====================================\n\n`;

    // 生成规则内容
    switch (type) {
        case 'currency':
            rule = generateCurrencyRules([item]);
            break;
        case 'equipment':
            rule = generateEquipmentRules([item]);
            break;
        case 'jewel':
            rule = generateJewelRules([item]);
            break;
        case 'flask':
            rule = generateFlaskRules([item]);
            break;
        case 'skillgem':
            rule = generateSkillGemRules([item]);
            break;
        case 'unique':
            rule = generateUniqueRules([item]);
            break;
        case 'normalequipment':
            rule = generateNormalEquipmentRules([item]);
            break;
        case 'misc':
            rule = generateMiscRules([item]);
            break;
        default:
            return '';
    }

    // 如果规则为空（可能是因为显示被禁用），则返回空字符串
    if (!rule) {
        return '';
    }

    // 组合完整的规则内容
    return header + ruleHeader + ruleTitle + ruleStatus + ruleDivider + rule;
}

export function generateFilterContent(
    currencyCheckedList: FilterItem[],
    equipmentCheckedList: FilterItem[],
    jewelCheckedList: FilterItem[],
    flaskCheckedList: FilterItem[],
    skillGemCheckedList: FilterItem[],
    uniqueCheckedList: FilterItem[],
    normalEquipmentCheckedList: FilterItem[],
    miscCheckedList: FilterItem[],
    globalCheckedList: FilterItem[]
): string {
    if (currencyCheckedList.length === 0 &&
        equipmentCheckedList.length === 0 &&
        jewelCheckedList.length === 0 &&
        flaskCheckedList.length === 0 &&
        skillGemCheckedList.length === 0 &&
        uniqueCheckedList.length === 0 &&
        normalEquipmentCheckedList.length === 0 &&
        miscCheckedList.length === 0 &&
        globalCheckedList.length === 0) {
        return "";
    }

    const header = generateHeader();

    // 生成其他规则
    const currencyRules = generateCurrencyRules(currencyCheckedList);
    const equipmentRules = generateEquipmentRules(equipmentCheckedList);
    const jewelRules = generateJewelRules(jewelCheckedList);
    const flaskRules = generateFlaskRules(flaskCheckedList);
    const skillGemRules = generateSkillGemRules(skillGemCheckedList);
    const uniqueRules = generateUniqueRules(uniqueCheckedList);
    const normalEquipmentRules = generateNormalEquipmentRules(normalEquipmentCheckedList);
    const miscRules = generateMiscRules(miscCheckedList);

    // 生成全局规则（应该放在最前面，因为它们是基础规则）
    const globalRules = globalCheckedList
        .map(item => generateRule(generateRuleWithState(item, 'global')))
        .filter(result => result.isValid && result.content.length > 0)
        .map(result => result.content)
        .join('\n\n');

    // 组合所有规则，确保全局规则在最前面
    const rules = [];

    // 首先添加全局规则（如果存在）
    if (globalRules.length > 0) {
        rules.push('# ==== 全局装备过滤规则 ====');
        rules.push(globalRules);
    }

    // 添加其他规则，每种类型都添加分隔注释
    if (currencyRules.length > 0) {
        rules.push('# ==== 通货过滤规则 ====');
        rules.push(currencyRules);
    }
    if (equipmentRules.length > 0) {
        rules.push('# ==== 地图过滤规则 ====');
        rules.push(equipmentRules);
    }
    if (jewelRules.length > 0) {
        rules.push('# ==== 珠宝过滤规则 ====');
        rules.push(jewelRules);
    }
    if (flaskRules.length > 0) {
        rules.push('# ==== 药剂过滤规则 ====');
        rules.push(flaskRules);
    }
    if (skillGemRules.length > 0) {
        rules.push('# ==== 技能石过滤规则 ====');
        rules.push(skillGemRules);
    }
    if (uniqueRules.length > 0) {
        rules.push('# ==== 传奇装备过滤规则 ====');
        rules.push(uniqueRules);
    }
    if (normalEquipmentRules.length > 0) {
        rules.push('# ==== 普通装备过滤规则 ====');
        rules.push(normalEquipmentRules);
    }
    if (miscRules.length > 0) {
        rules.push('# ==== 杂项过滤规则 ====');
        rules.push(miscRules);
    }

    // 合并所有规则，添加头部信息
    return header + rules.join('\n\n');
}